﻿Imports System.Data.SqlClient

Public Class clsPEDIDOS
    Private mintResultado As Integer

    Public Property Resultado() As Integer
        Get
            Return mintResultado
        End Get
        Set(ByVal value As Integer)
            mintResultado = value
        End Set
    End Property
    Function INSERTAR_PEDIDO_GENERALES(ByVal DESCRIPCION As String, ByVal NO_PEDIDO As Long, ByVal CEDIS As String, ByVal CONDICIONES As String, ByVal TRANSPORTE As String, ByVal FECHA_INICIO As Date, ByVal FECHA_FIN As Date, ByVal SEMANA_INICIAL As Integer, ByVal SEMANA_FINAL As Integer, ByVal TIPO_PEDIDO As String, ByVal RECEPCION As Date) As Integer


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand("INSERTAR_PEDIDO", cnConn)
        Command.CommandType = CommandType.StoredProcedure

        Command.Parameters.Add(New SqlParameter("@DESCRIPCION", SqlDbType.VarChar, 200))
        Command.Parameters("@DESCRIPCION").Value = DESCRIPCION
        Command.Parameters.Add(New SqlParameter("@NO_PEDIDO", SqlDbType.BigInt))
        Command.Parameters("@NO_PEDIDO").Value = NO_PEDIDO
        Command.Parameters.Add(New SqlParameter("@CEDIS", SqlDbType.VarChar, 500))
        Command.Parameters("@CEDIS").Value = CEDIS
        Command.Parameters.Add(New SqlParameter("@CONDICIONES", SqlDbType.VarChar, 200))
        Command.Parameters("@CONDICIONES").Value = CONDICIONES
        Command.Parameters.Add(New SqlParameter("@TRANSPORTE", SqlDbType.VarChar, 200))
        Command.Parameters("@TRANSPORTE").Value = TRANSPORTE
        Command.Parameters.Add(New SqlParameter("@FECHA_INICIO", SqlDbType.DateTime))
        Command.Parameters("@FECHA_INICIO").Value = FECHA_INICIO
        Command.Parameters.Add(New SqlParameter("@FECHA_FINAL", SqlDbType.DateTime))
        Command.Parameters("@FECHA_FINAL").Value = FECHA_FIN
        Command.Parameters.Add(New SqlParameter("@SEMANA_RECEPCION", SqlDbType.Int))
        Command.Parameters("@SEMANA_RECEPCION").Value = SEMANA_INICIAL
        Command.Parameters.Add(New SqlParameter("@SEMANA_FINAL", SqlDbType.Int))
        Command.Parameters("@SEMANA_FINAL").Value = SEMANA_FINAL
        Command.Parameters.Add(New SqlParameter("@TIPO_PEDIDO", SqlDbType.VarChar, 50))
        Command.Parameters("@TIPO_PEDIDO").Value = TIPO_PEDIDO
        Command.Parameters.Add(New SqlParameter("@RECEPCION", SqlDbType.DateTime))
        Command.Parameters("@RECEPCION").Value = RECEPCION

        Dim Lector As SqlDataReader

        Try
            cnConn.Open()
            Lector = Command.ExecuteReader

            If Lector.Read() Then
                INSERTAR_PEDIDO_GENERALES = Lector("IDPEDIDO")
                mintResultado = Lector("RESULTADO")
            End If

        Catch ex As Exception
            'MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try


    End Function
    Sub ACTUALIZA_DATOS_CITA(ByVal IDPEDIDO As Integer, ByVal CITA_CONFIRMACION As Date, ByVal NO_CONFIRMACION As String)
        Dim sql As String = "UPDATE PEDIDOS SET " & _
                           "CITA_CONFIRMACION=@CITA_CONFIRMACION," & _
                           "NO_CONFIRMACION=@NO_CONFIRMACION " & _
                           "WHERE IDPEDIDO=@IDPEDIDO"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("CITA_CONFIRMACION", CITA_CONFIRMACION)
        Command.Parameters.AddWithValue("NO_CONFIRMACION", NO_CONFIRMACION)
        Command.Parameters.AddWithValue("IDPEDIDO", IDPEDIDO)


        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
            If RowsAffected > 0 Then
                cnConn.Close()
            Else
                If cnConn.State = ConnectionState.Open Then
                    cnConn.Close()
                End If
            End If
        Catch ex As Exception
            ' MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
        End Try

    End Sub
    Function INSERTAR_PEDIDO_DETALLE(ByVal IDPEDIDO As Integer, ByVal TIPO_ESTILO As String, ByVal LINEA As String, ByVal UPC As Long, ByVal ESTILO As String, ByVal EMPAQUES As Double, ByVal DESCRIPCION As String) As Integer
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand("INSERTAR_PEDIDO_DETALLE", cnConn)
        Command.CommandType = CommandType.StoredProcedure

        Command.Parameters.Add(New SqlParameter("@IDPEDIDO", SqlDbType.Int))
        Command.Parameters("@IDPEDIDO").Value = IDPEDIDO
        Command.Parameters.Add(New SqlParameter("@TIPO_ESTILO", SqlDbType.VarChar, 100))
        Command.Parameters("@TIPO_ESTILO").Value = TIPO_ESTILO
        Command.Parameters.Add(New SqlParameter("@LINEA", SqlDbType.VarChar, 100))
        Command.Parameters("@LINEA").Value = LINEA
        Command.Parameters.Add(New SqlParameter("@UPC", SqlDbType.BigInt))
        Command.Parameters("@UPC").Value = UPC
        Command.Parameters.Add(New SqlParameter("@ESTILO", SqlDbType.VarChar, 200))
        Command.Parameters("@ESTILO").Value = ESTILO
        Command.Parameters.Add(New SqlParameter("@EMPAQUES", SqlDbType.Decimal))
        Command.Parameters("@EMPAQUES").Value = EMPAQUES
        Command.Parameters.Add(New SqlParameter("@DESCRIPCION", SqlDbType.VarChar, 200))
        Command.Parameters("@DESCRIPCION").Value = DESCRIPCION

        Dim Lector As SqlDataReader

        Try
            cnConn.Open()
            Lector = Command.ExecuteReader
            If Lector.Read() Then
                INSERTAR_PEDIDO_DETALLE = Lector("IDDETALLE")
            End If

        Catch ex As Exception
            ' MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try


    End Function
    Sub ACTUALIZA_ENTREGADAS(ByVal IDPEDIDO As Integer, ByVal IDDETALLE As Integer, ByVal ENTREGADAS As Double)
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand("ACTUALIZA_PEDIDO", cnConn)
        Command.CommandType = CommandType.StoredProcedure

        Command.Parameters.Add(New SqlParameter("@IDPEDIDO", SqlDbType.Int))
        Command.Parameters("@IDPEDIDO").Value = IDPEDIDO
        Command.Parameters.Add(New SqlParameter("@IDDETALLE", SqlDbType.Int))
        Command.Parameters("@IDDETALLE").Value = IDDETALLE
        Command.Parameters.Add(New SqlParameter("@ENTREGADAS", SqlDbType.Decimal))
        Command.Parameters("@ENTREGADAS").Value = ENTREGADAS
        
        Dim Lector As SqlDataReader

        Try
            cnConn.Open()
            Lector = Command.ExecuteReader
        Catch ex As Exception
            'MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Enum CAMPO_ACTUALIZAR
        FOLIO_GUIA = 1
        FECHA_ENTREGA_LOG = 2
        SEMANA_ENTREGA_LOG = 3
        FECHA_ENTREGA_CEDIS = 4
        ESTATUS = 5
        OBSERVACIONES = 6
    End Enum
    Sub ACTUALIZA_CAMPO(ByVal CAMPO As CAMPO_ACTUALIZAR, ByVal CAMPOSTR As String, ByVal CAMPODAT As Date, ByVal IDDETALLE As Integer)
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection
        Dim Command As SqlCommand
        Dim sql As String = ""
        Select Case CAMPO
            Case CAMPO_ACTUALIZAR.FOLIO_GUIA
                sql = "UPDATE PEDIDOS_DETALLE SET FOLIO_GUIA=@CAMPO WHERE IDDETALLE=@IDDETALLE"
                Command = New SqlCommand(sql, cnConn)
                Command.Parameters.AddWithValue("CAMPO", CAMPOSTR)
            Case CAMPO_ACTUALIZAR.FECHA_ENTREGA_LOG
                sql = "UPDATE PEDIDOS_DETALLE SET FECHA_ENTEGA_LOG=@CAMPO WHERE IDDETALLE=@IDDETALLE"
                Command = New SqlCommand(sql, cnConn)
                Command.Parameters.AddWithValue("CAMPO", CAMPODAT)
            Case CAMPO_ACTUALIZAR.SEMANA_ENTREGA_LOG
                sql = "UPDATE PEDIDOS_DETALLE SET SEMANA_ENTREGA_LOG=@CAMPO WHERE IDDETALLE=@IDDETALLE"
                Command = New SqlCommand(sql, cnConn)
                Command.Parameters.AddWithValue("CAMPO", CAMPOSTR)
            Case CAMPO_ACTUALIZAR.FECHA_ENTREGA_CEDIS
                sql = "UPDATE PEDIDOS_DETALLE SET FECHA_ENTREGA_CEDIS=@CAMPO WHERE IDDETALLE=@IDDETALLE"
                Command = New SqlCommand(sql, cnConn)
                Command.Parameters.AddWithValue("CAMPO", CAMPODAT)
            Case CAMPO_ACTUALIZAR.ESTATUS
                sql = "UPDATE PEDIDOS_DETALLE SET ESTATUS=@CAMPO WHERE IDDETALLE=@IDDETALLE"
                Command = New SqlCommand(sql, cnConn)
                Command.Parameters.AddWithValue("CAMPO", CAMPOSTR)
            Case CAMPO_ACTUALIZAR.OBSERVACIONES
                sql = "UPDATE PEDIDOS_DETALLE SET OBSERVACIONES=@CAMPO WHERE IDDETALLE=@IDDETALLE"
                Command = New SqlCommand(sql, cnConn)
                Command.Parameters.AddWithValue("CAMPO", CAMPOSTR)
        End Select

       




        Command.Parameters.AddWithValue("IDDETALLE", IDDETALLE)

        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
            If RowsAffected > 0 Then
                cnConn.Close()
            Else
                If cnConn.State = ConnectionState.Open Then
                    cnConn.Close()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
        End Try

    End Sub
End Class
